package com.shenda.api.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.Lists;
import com.shenda.api.dao.GyroDao;
import com.shenda.api.dto.ConfigDTO;
import com.shenda.api.dto.OnlinesDTO;
import com.shenda.api.entity.Gyro;
import com.shenda.api.service.GyroService;
import com.shenda.device.Device;
import com.shenda.device.Devices;
import com.swak.annotation.FluxService;
import com.swak.entity.Page;
import com.swak.entity.Parameters;
import com.swak.persistence.QueryCondition;
import com.swak.persistence.QueryCondition.Criteria;
import com.swak.rxtx.Channels;
import com.swak.utils.Ints;
import com.swak.utils.StringUtils;

/**
 * 陀螺仪服务
 * 
 * @author lifeng
 */
@FluxService
public class GyroServiceImpl implements GyroService {

	@Autowired
	private GyroDao groDao;

	/**
	 * 分页查询
	 * 
	 * @param query
	 * @param param
	 * @return
	 */
	public Page page(Gyro query, Parameters param) {
		QueryCondition qc = new QueryCondition();
		Criteria criteria = qc.getCriteria();
		if (StringUtils.isNotBlank(query.getName())) {
			criteria.andEqualTo("NAME", query.getName());
		}
		return groDao.page(qc, param);
	}

	/**
	 * 在线设备
	 */
	@Override
	public OnlinesDTO onlines() {
		List<Gyro> onlines = Lists.newArrayList();
		List<Device> devices = Devices.me().devices();
		for (Device device : devices) {
			Gyro gyro = this.groDao.get(device.getMac());
			if (gyro == null) {
				gyro = new Gyro();
			}
			gyro.setId(device.getMac());
			if (StringUtils.isNotBlank(device.getMac())) {
				gyro.setName(device.getGyroName());
			}
			if (StringUtils.isNotBlank(gyro.getCjpl())) {
				gyro.setCjpl(String.valueOf(Ints.decodeHex(gyro.getCjpl())));
			}
			gyro.setStatus(device.getStatus().name());
			gyro.setUploads(device.uploadSurvey());
			onlines.add(gyro);
		}

		return new OnlinesDTO().setRunning(Channels.me().isRunning()).setOnlines(onlines);
	}

	/**
	 * 入库
	 */
	@Override
	public void save(Gyro query) {
		this.groDao.insert(query);
	}

	/**
	 * 修改配置
	 */
	@Override
	public void updateSettings(Gyro query) {
		this.groDao.updateSettings(query);
	}

	/**
	 * 修改檫除结果
	 */
	@Override
	public void updateFlush(Gyro query) {
		this.groDao.updateFlush(query);
	}

	/**
	 * 重新连接设备
	 */
	@Override
	public void refresh() {
		Devices.me().connect();
	}

	/**
	 * 目前还需要发送配置的参数
	 */
	@Override
	public void config(ConfigDTO config) {

		// 采集频率
		int cjpl = Integer.parseInt(config.getCjpl());

		// 采集频率 16 进制
		String strCjpl = Ints.encodeHex(cjpl);

		// 补0
		strCjpl = StringUtils.leftPad(strCjpl, 4, "0");

		// odr 和采集频率一致
		String strOdr = strCjpl;

		// 发送命令
		Devices.me().deviceConfig(config.getCjms(), strCjpl, strOdr);
	}

	/**
	 * 开始采集数据
	 */
	@Override
	public void gather() {
		Devices.me().startCollection();
	}

	/**
	 * 停止采集数据
	 */
	@Override
	public void ungather() {
		Devices.me().stopCollection();
	}

	/**
	 * 开始上传
	 */
	@Override
	public void upload() {
		Devices.me().startUpload();
	}

	/**
	 * 停止上传
	 */
	@Override
	public void unupload() {
		Devices.me().stopUpload();
	}

	/**
	 * 清除数据
	 */
	@Override
	public void flush() {
		Devices.me().flush();
	}

	/**
	 * 重启
	 */
	@Override
	public void reboot() {
		Devices.me().reboot();
	}

	/**
	 * 关闭
	 */
	@Override
	public void close() {
		Devices.me().close();
	}
}